package class10;

import class10.TreeTools.*;

/**
 * <a href="https://leetcode.cn/problems/binary-tree-preorder-traversal/">144. 二叉树的前序遍历</a>
 * <a href="https://leetcode.cn/problems/binary-tree-inorder-traversal/">94. 二叉树的中序遍历</a>
 * <a href="https://leetcode.cn/problems/binary-tree-postorder-traversal/">145. 二叉树的后序遍历</a>
 */
public class Code02_RecursiveTraversalBT {

    // 二叉树的递归序
    public static void f(TreeNode head) {
        if (head == null) {
            return;
        }
        // 1 先
        f(head.left);
        // 2 中
        f(head.right);
        // 3 后
    }

    // 二叉树先序遍历
    public static void pre(TreeNode head) {
        if (head == null) {
            return;
        }
        System.out.printf("%d ", head.value);
        pre(head.left);
        pre(head.right);
    }

    // 二叉树中序遍历
    public static void in(TreeNode head) {
        if (head == null) {
            return;
        }
        in(head.left);
        System.out.printf("%d ", head.value);
        in(head.right);
    }

    // 二叉树后序遍历
    public static void pos(TreeNode head) {
        if (head == null) {
            return;
        }
        pos(head.left);
        pos(head.right);
        System.out.printf("%d ", head.value);
    }

    public static void main(String[] args) {
        TreeNode treeNode = TreeTools.stringToTreeNode("[1,2,3,4,5,6,7,8]");
        pre(treeNode);
        System.out.println();
        in(treeNode);
        System.out.println();
        pos(treeNode);
    }

}
